package com.gkr.shopping.serviceimpl;

import java.util.Date;
import java.util.Properties;
import java.util.UUID;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gkr.shopping.bean.User;
import com.gkr.shopping.mapper.UserMapper;
import com.gkr.shopping.service.UserService;
import com.sun.mail.smtp.SMTPMessage;


@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	//根据用户输入的账号以及密码获取用户的信息
	@Override
	public User findUserByNameAndPass(User user) {
		// TODO Auto-generated method stub
		User u = userMapper.findUserByNameAndPass(user);
		return u;
	}

	@Override
	public String validLoginName(String loginName) {
		// TODO Auto-generated method stub
		
		User user = userMapper.validLoginName(loginName);
		if(user != null) {
			return "您输入的账号已经存在！";
		}
		return "";
	}
	//用户注册
	@Override
	public void saveUser(User user,HttpServletRequest request) {
		// TODO Auto-generated method stub
		try {
			String active = UUID.randomUUID().toString();
			user.setCreateDate(new Date());
			user.setActive(active);
			userMapper.saveUser(user);

			//创建Properties对象
			Properties props = new Properties();
			//设置邮件服务器的地址
			props.setProperty("mail.smtp.host", "smtp.126.com");

			props.setProperty("mail.smtp.auth", "true");
			
			//创建Authenticator的实例
	        Authenticator auth = new Authenticator(){
	            protected PasswordAuthentication getPasswordAuthentication()
	            {
	                return new PasswordAuthentication("gkr1326271157@126.com", "MVCPCLFYVDMSDCQE");
	            }
	        };
		
				//通过Session与服务器建立连接
				Session session = Session.getInstance(props,auth);
				
				//对象
				SMTPMessage message = new SMTPMessage(session);
				
				//主题
				message.setSubject("用户注册激活邮件，请勿回复，按照指引激活");
				//内容
				message.setContent("<a href='http://127.0.0.1:8080"+request.getContextPath()+"/user/active?activeCode="+user.getActive()+"' target='_blank'>恭喜您注册成功，请点击该链接进行激活，无需回复！</a>", "text/html;charset=utf-8");
				
				//发件人
				message.setFrom(new InternetAddress("gkr1326271157@126.com"));
				
				//收件人
				message.setRecipient(RecipientType.TO, new InternetAddress(user.getEmail()));
				
				//发送邮件
				Transport.send(message);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		

		
	}

	//用户信息激活
	@Override
	public String active(String activeCode) {
		// TODO Auto-generated method stub
		//根据激活码获取用户信息，如果找不到说明  激活码不正确，或者已经激活过
		User user = userMapper.getUserByActive(activeCode);
		if(user!=null) {
			userMapper.active(activeCode);
			return "";
		}else {
			return "激活码失效！";
		}
	}
	
	
	public static void main(String[] args) {
		
		try {
			
			//1、创建Properties对象
			Properties props = new Properties();
			//设置邮件服务器的地址
			props.setProperty("mail.smtp.host", "smtp.126.com");

			props.setProperty("mail.smtp.auth", "true");
			
			//2、使用账号：gkr1326271157@126.com   密码：gkr970421?! 登录至邮件服务器
	        Authenticator auth = new Authenticator(){
	            protected PasswordAuthentication getPasswordAuthentication()
	            {
	                return new PasswordAuthentication("gkr1326271157@126.com", "gkr970421?!");
	            }
	        };
		
				//3、通过Session与服务器建立连接
				Session session = Session.getInstance(props,auth);
				
				//4、创建SMTPMessage对象
				SMTPMessage message = new SMTPMessage(session);
				
				//主题
				message.setSubject("用户注册激活邮件，请勿回复，按照指引激活");
				//内容
				message.setContent("<a href='http://127.0.0.1:8080/ecShop/user/active?activeCode="+UUID.randomUUID().toString()+"' target='_blank'>恭喜您注册成功，请点击该链接进行激活，无需回复！</a>", "text/html;charset=utf-8");
				
				//发件人
				message.setFrom(new InternetAddress("gkr1326271157@126.com"));
				
				//收件人地址
				message.setRecipient(RecipientType.TO, new InternetAddress("1326271157@qq.com"));
				
				//5、发送邮件
				Transport.send(message);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
